package com.foreknow.example;

import java.util.Scanner;

public class Test {

    /**
     * 输出图书信息
     */
    public void printBooksInfo() {
        Book[] books = new Book[3]; //创建一个数组容器
        books[0] = new Book(1,"Java",33.45,50);
        books[1] = new Book(2,"Jsp",133.45,150);
        books[2] = new Book(3,"PHP",55.45,200);

        for (int i = 0;i<books.length;i++) {
            Book book = books[i];
            System.out.println(book.getBid()+"--"+book.getBname()+"--"+book.getPrice()+"--"+book.getStorage());
        }
    }

    /**
     * 购买图书
     */

    public void buyBooks(Book[] books) {
        Scanner input = new Scanner(System.in);
        Order order = new Order("0001");
        for (int i = 0;i<3;i++) {
            System.out.println("请输入图书的编号: ");
            int pid = input.nextInt();
            System.out.println("请输入购买数量: ");
            int num = input.nextInt();
            //创建OrderItem对象
            OrderItem item = new OrderItem(books[pid-1].getBname(),books[pid-1].getPrice(),num);
            //将对象OrderItem添加到数组中
            order.setItem(item,i);
        }
        System.out.println(order.getOrderId());
        System.out.println(order.getTotal());
        //从订单对象中获取到数组
        //遍历OrderItems数组并输出购买的商品信息
        OrderItem[] items = order.getItems();
        for(int i = 0;i<items.length;i++) {
            System.out.println(items[i].getPname()+"--"+items[i].getPprice()+"--"+items[i].getNum());
        }
    }


    public static void main(String[] args) {
        Test test = new Test();
        test.printBooksInfo();
        Book[] books = new Book[3]; //创建一个数组容器
        books[0] = new Book(1,"Java",33.45,50);
        books[1] = new Book(2,"Jsp",133.45,150);
        books[2] = new Book(3,"PHP",55.45,200);
        test.buyBooks(books);
    }
}
